# Adogs
# 时间: 2021/7/17 10:47
import pygame
from actor import WalkByImage
from actor import WalkDir


class WalkSoldier(pygame.sprite.Sprite):
    """
    行走战士类
    方法：
    1.__init__(walk_imgpath, pos_x, pos_y, name, hp)
    2.run(down_flag, key_list, obstacle_group)
    3.draw(surface, x, y)
    """
    hp = 100
    name = '士兵'
    pos_x = 200
    pos_y = 300
    image_wide = 50
    image_high = 100
    walk_action = None
    rect = ()
    '''人物基本参数'''

    def __init__(self, walk_imgpath: str, pos_x, pos_y, name, hp):
        """
        初始化函数
        :param walk_imgpath: 图片路径
        :param pos_x: 人物x坐标
        :param pos_y: 人物y坐标
        :param name: 人物名字
        :param hp: 人物血量
        """
        super(WalkSoldier, self).__init__()
        self.run_flag = False
        self.walk_action = WalkByImage(walk_imgpath, self.image_wide, self.image_high, True)    # 分割图片
        self.walk_action.set_dir(WalkDir.down)  # 设置方向
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.name = name
        self.hp = hp
        self.rect = pygame.Rect(self.pos_x, self.pos_y, self.image_wide, self.image_high)

    def run(self, down_flag, key_list, obstacle_group: pygame.sprite.Group):
        """
        人物运动函数
        :param down_flag: 判断是否点击按键
        :param key_list: 判断是否有键按下
        :param obstacle_group: 障碍物组
        :return: 无返回值
        """
        if key_list[pygame.K_s]:
            self.walk_action.set_dir(WalkDir.down)
            self.pos_y += 10
            self.run_flag = True
        elif key_list[pygame.K_w]:
            self.walk_action.set_dir(WalkDir.on)
            self.pos_y -= 10
            self.run_flag = True
        elif key_list[pygame.K_a]:
            self.walk_action.set_dir(WalkDir.left)
            self.pos_x -= 10
            self.run_flag = True
        elif key_list[pygame.K_d]:
            self.walk_action.set_dir(WalkDir.right)
            self.pos_x += 10
            self.run_flag = True

    def draw(self, surface: pygame.Surface, x: int, y: int):
        """
        绘制函数
        :param surface: 绘制屏幕
        :param x: 屏幕相对x坐标
        :param y: 屏幕相对y坐标
        :return: 无返回值
        """
        current_image = self.walk_action.get_image(self.run_flag)
        surface.blit(current_image, (self.pos_x - x, self.pos_y - y))
        rect_x, rect_y, w, h = self.rect
        # pygame.draw.rect(surface, pygame.Color(255, 255, 0), (rect_x-x, rect_y-y, w, h), 1)
